#include <vector>
#include <memory>
#include <iostream>
#include <algorithm>
#include "student.h"

std::vector<std::unique_ptr<student>> students;

void add_student(){
    std::string name;
    int id;

    std::cout << "请输入学生姓名：";
    std::getline(std::cin, name);

    std::cout<<"请输入学生学号：";
    std::cin>>id;
    std::cin.ignore();

    //使用make_unque创建对象
    students.emplace_back(std::make_unique<student>(id,name));
    std::cout<<"学生添加成功\n";

}

void delete_student(){
    int targetId;
    std::cout<<"请输入要删除的学生学号：";
    std::cin>>targetId;

    auto it = std::remove_if(students.begin(),students.end(),
    [targetId](const auto& s){
        return s->id() == targetId;
    }
    );

    if(it!=students.end()){
        students.erase(it,students.end());  
        std::cout<<"找到了学生："<<((*it)->name())<<std::endl<<"学生删除成功\n";
    }else{
        std::cout<<"未找到该学生\n";
    }
}



//查询学生用id,用姓名
void search_student(){
    int targetId;
    std::cout<<"请输入要查找的学生学号：";
    std::cin>>targetId;

    auto it = std::find_if(students.begin(),students.end(),
    [targetId](const auto& s){
        return s->id() == targetId;
    }
    );

    if(it != students.end()){
        std::cout<<"找到了学生："<<((*it)->name())<<std::endl;
        std::cout<<"学生ID: ";
    }else{
        std::cout<<"未找到该学生\n";
    }
}




int main(){
    int choice;
    while(true){

        std::cout<<"1.添加学生\n";
        std::cout<<"2.删除学生\n";
        std::cout<<"3.退出\n";
        std::cin>>choice;
        std::cin.ignore();

        switch(choice){
            case 1:
                add_student();
                break;
            case 2:
                delete_student();
                break;
            case 3:
                return 0;
        
            default:
                std::cout<<"无效的选择\n";
        }
    }
}